package com.lws.systembarsample

import android.app.Activity
import android.app.Application
import android.content.res.TypedArray
import android.os.Bundle
import androidx.core.view.ViewCompat

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        registerActivityLifecycleCallbacks(SystemBarActivityLifecycleCallbacks)
    }
}

object SystemBarActivityLifecycleCallbacks : Application.ActivityLifecycleCallbacks {
    private val lightThemeAttributes = intArrayOf(R.attr.isLightTheme)
    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        val lightThemeTypedArray: TypedArray = activity.obtainStyledAttributes(lightThemeAttributes)
        val isLightTheme = lightThemeTypedArray.getBoolean(0, true)
        lightThemeTypedArray.recycle()

        ViewCompat.getWindowInsetsController(activity.findViewById(android.R.id.content))?.apply {
            isAppearanceLightStatusBars = isLightTheme
            isAppearanceLightNavigationBars = isLightTheme
        }
    }

    override fun onActivityStarted(activity: Activity) {

    }

    override fun onActivityResumed(activity: Activity) {

    }

    override fun onActivityPaused(activity: Activity) {

    }

    override fun onActivityStopped(activity: Activity) {

    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

    }

    override fun onActivityDestroyed(activity: Activity) {

    }
}